﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using Eassets.Layered.Domain.Asset;
using Eassets.Service.Asset;
using Eassets.Service;
using System.Collections.ObjectModel;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;

namespace Eassets.UI.Asset
{
    public partial class FixedAssetItemDetailsForm : System.Web.UI.Page
    {
        int i;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                textBoxPurchasedValue.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");
                textBoxDepreciationValue.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");
                textBoxCumilativeDepValue.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");
                textBoxCurrentBookValue.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");
                textBoxRemainingLifeTime.Attributes.Add("onkeypress", "javascript:return handleNumbersOnly(event,this)");


                string parameters = Request.QueryString.Get("Parameter");
                //string parameterProcurement = Request.QueryString.Get("ParameterProcurement");
                if (parameters != null)
                {
                    Regex regex = new Regex(";");
                    string[] arrayParameters = regex.Split(parameters);

                    long mainCategoryId = Convert.ToInt64(arrayParameters[1]);
                    long subCategoryId = Convert.ToInt64(arrayParameters[2]);


                    CategoryService categoryService = new CategoryService();
                    List<Category> category = new List<Category>();
                    category = categoryService.GetAllAssetCategory().ToList();
                    DataBindToMainCategory(category.Where(x => x.ParentId == 0).ToList(), mainCategoryId);
                    DataBindToSubCategory(category.Where(x => x.ParentId == mainCategoryId).ToList(), subCategoryId);
                    textBoxAsset.Text = arrayParameters[3];
                    textBoxnoOfItems.Text = arrayParameters[4];
                    ViewState["NumberOfAssets"] = arrayParameters[4];
                    GetDepriciationDetails(Convert.ToInt64(arrayParameters[0]));
                    ViewState["AssetId"] = arrayParameters[0];
                    ViewState["AssetItemDetails"] = new Collection<AssetItemDetails>();

                    ViewState["IsProcurement"] = arrayParameters.Length > 5 && arrayParameters[5].Trim() == "PR" ? true : false;
                    ViewState["OLA"] = arrayParameters[5].Trim();
                    ViewState["OrganizationLevelListId"] = Convert.ToInt64(arrayParameters[6]);
                    GetData(Convert.ToInt16(arrayParameters[4]), Convert.ToInt64(arrayParameters[0]), DataBindToGridView(), Convert.ToBoolean(ViewState["IsProcurement"]));

                    if (arrayParameters[5].Trim() == "PR")
                    {
                        DisableComponants();
                    }

                    GetOrganizationLevelListname();
                }
            }

        }

        private void GetOrganizationLevelListname()
        {
            OrganizationLevelListService OrganizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            organizationLevelLists = OrganizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelListId == Convert.ToInt64(ViewState["OrganizationLevelListId"])).ToList();

            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataTextField = "Description";
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataBind();
        }

        private void DisableComponants()
        {
            textBoxPurchasedDate.Text = string.Empty;
            textBoxPurchasedValue.Text = string.Empty;
            textBoxDepreciationValue.Text = string.Empty;
            textBoxCumilativeDepValue.Text = string.Empty;
            textBoxCurrentBookValue.Text = string.Empty;
            textBoxRemainingLifeTime.Text = string.Empty;

            textBoxPurchasedDate.Enabled = false;
            textBoxPurchasedValue.Enabled = false;
            textBoxDepreciationValue.Enabled = false;
            textBoxCumilativeDepValue.Enabled = false;
            textBoxCurrentBookValue.Enabled = false;
            textBoxRemainingLifeTime.Enabled = false;
        }

        private void EnableComponants()
        {
            textBoxPurchasedDate.Enabled = true;
            textBoxPurchasedValue.Enabled = true;
            textBoxDepreciationValue.Enabled = true;
            textBoxCumilativeDepValue.Enabled = true;
            textBoxCurrentBookValue.Enabled = true;
            textBoxRemainingLifeTime.Enabled = true;
        }

        protected void gridViewItemDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gridViewItemDetails.DataSource = (Collection<AssetItemDetails>)ViewState["AssetItemDetails"];
            gridViewItemDetails.DataBind();
            //  GetData(Convert.ToInt16(textBoxnoOfItems.Text), Convert.ToInt64(ViewState["AssetId"]), DataBindToGridView(), Convert.ToBoolean(ViewState["IsProcurement"]));
            gridViewItemDetails.PageIndex = e.NewPageIndex;
            gridViewItemDetails.DataBind();
        }


        protected void textBoxPurchaseDate_OnTextChange(object sender, EventArgs e)
        {
            TextBox txtBoxRow = (TextBox)sender;

            GridViewRow row = (GridViewRow)((TextBox)sender).Parent.Parent;

            if (row != null)
            {
                TextBox textBoxPurchaseDate = (TextBox)row.Cells[1].FindControl("textBoxPuDate");

                Label labelRemainingLifeTime = (Label)row.Cells[8].FindControl("labelDepreciationRate");
                int remainingLifeTime = Convert.ToInt16(row.Cells[5].Text);
                decimal dateDifferent;
                dateDifferent = Convert.ToDecimal(CommonClass.DifferenceTotalMonth(Convert.ToDateTime(textBoxPurchaseDate.Text), DateTime.Today));

            }
        }

        protected void checkBoxHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gridViewRow in ((GridView)((CheckBox)sender).Parent.Parent.Parent.Parent).Rows)
            {
                ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked = ((CheckBox)sender).Checked;


            }
        }

        protected void checkBoxItem_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gridViewRow in ((GridView)((CheckBox)sender).Parent.Parent.Parent.Parent).Rows)
            {
                if (Convert.ToBoolean(ViewState["IsProcurement"]))
                {
                    if (gridViewRow.Cells[1].Text == "0")
                    {
                        ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked = ((CheckBox)sender).Checked;
                        EnableComponants();
                    }
                }
                else
                {
                    ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked = ((CheckBox)sender).Checked;

                }


            }
        }


        private void GetData(int count, long AssetId, Collection<AssetItemDetails> assetItemDetails, Boolean IsProc)
        {
            DataBindToGridView(CreateGridView(count, AssetId, assetItemDetails, IsProc));
        }



        private void DataBindToGridView(Collection<AssetItemDetails> assetItemDetails)
        {

            ViewState["AssetItemDetails"] = assetItemDetails;
            gridViewItemDetails.DataSource = assetItemDetails;
            gridViewItemDetails.DataBind();
            if (Convert.ToBoolean(ViewState["IsProcurement"]))
            {
                DisableCheckBox();
            }

        }

        private void DisableCheckBox()
        {
            foreach (GridViewRow gridViewRow in gridViewItemDetails.Rows)
            {
                if (gridViewRow.Cells[1].Text != "0")
                {
                    ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Enabled = false;
                }

            }
        }

        private Collection<AssetItemDetails> CreateGridView(int count, long AssetId, Collection<AssetItemDetails> currentAssetDetails, Boolean? all)
        {
            Collection<AssetItemDetails> assetItemDetails = new Collection<AssetItemDetails>();
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();

            assetItemDetails = currentAssetDetails;
            if (all != null && Convert.ToBoolean(all))
            {
                count = count;
            }
            else
            {
                count = count - currentAssetDetails.Count;
            }


            count = count < 0 ? 0 : count;
            //int x = currentAssetDetails.Count;
            ViewState["currentAssetDetails"] = currentAssetDetails.Count;
            int serialStart = 0;
            List<int> val = (from assetSerial in assetItemDetailsService.GetAllAssetItemDetails().Where(x => x.AssetId == Convert.ToInt16(ViewState["AssetId"])) select Convert.ToInt32(assetSerial.AssetSerialCode)).ToList();

            if ((val.Count > 0 && val.Max() > 0))
            {
                serialStart = val.Max();
            }
            //int i;
            if (currentAssetDetails.Count > 0)
            {
                DisableComponants();
                //buttonSave.Enabled = false;

                //i = i + count;
                for (i = 0; i < count; i++)
                {
                    serialStart++;
                    AssetItemDetails assetItemDetail = new AssetItemDetails();


                    string s = string.Empty;
                    if (serialStart < 10)
                    {
                        s = "0000";
                    }
                    else if (serialStart < 100)
                    {
                        s = "000";
                    }
                    else if (serialStart < 1000)
                    {
                        s = "00";
                    }
                    else if (serialStart < 10000)
                    {
                        s = "0";
                    }
                    assetItemDetail.AssetSerialCode = s + serialStart;
                    //assetItemDetail.AssetSerialCode = "0000" + (serialStart);

                    CompanyAssetService CompanyAssetService = new CompanyAssetService();
                    CompanyAsset companyAsset = new CompanyAsset();

                    companyAsset = CompanyAssetService.GetCompanyAssetByAssetId(AssetId);

                    ViewState["RemainingLifeTime"] = companyAsset.LifeTime;
                    assetItemDetail.Warranty = String.Empty;
                    assetItemDetail.Barcode = String.Empty;

                    assetItemDetails.Add(assetItemDetail);
                    //i = (i + 1);
                }
            }
            else
            {

                for (int i = 0; i < count; i++)
                {
                    AssetItemDetails assetItemDetail = new AssetItemDetails();
                    assetItemDetail.AssetSerialCode = "0000" + (i + 1);

                    CompanyAssetService CompanyAssetService = new CompanyAssetService();
                    CompanyAsset companyAsset = new CompanyAsset();
                    companyAsset = CompanyAssetService.GetCompanyAssetByAssetId(AssetId);

                    ViewState["RemainingLifeTime"] = companyAsset.LifeTime;
                    assetItemDetail.Warranty = String.Empty;
                    assetItemDetail.Barcode = String.Empty;

                    assetItemDetails.Add(assetItemDetail);
                }
                return assetItemDetails;

            }
            return assetItemDetails;
        }

        protected void PurchaseValue_OnTextChange(object sender, EventArgs e)
        {
            if (textBoxPurchasedValue.Text != "")
            {
                Decimal depreciationValue;

                depreciationValue = (Convert.ToDecimal(textBoxPurchasedValue.Text.Trim()) * (Convert.ToDecimal(textBoxDepreciationRate.Text.Trim())) / 1200);
                textBoxDepreciationValue.Text = Math.Round(depreciationValue, 5).ToString();

                if (textBoxCumilativeDepValue.Text != String.Empty)
                {
                    textBoxCurrentBookValue.Text = (Convert.ToDecimal(textBoxPurchasedValue.Text) - Convert.ToDecimal(textBoxCumilativeDepValue.Text)).ToString();
                }

                if (textBoxPurchasedDate.Text != string.Empty)
                {
                    //TimeSpan dateDiff = Convert.ToDateTime(textBoxPurchasedDate.Text).Subtract(Convert.ToDateTime(DateTime.Today));
                    TimeSpan dateDiff = Convert.ToDateTime(DateTime.Today).Subtract(Convert.ToDateTime(textBoxPurchasedDate.Text));
                    long dateDifferent = (dateDiff.Days) / 30;
                    ViewState["DateDifferent"] = dateDifferent;
                    //textBoxRemainingLifeTime.Text = (Convert.ToInt16((ViewState["RemainingLifeTime"])) - Convert.ToInt16(dateDifferent));
                    CompanyAssetService CompanyAssetService = new CompanyAssetService();
                    CompanyAsset companyAsset = new CompanyAsset();

                    companyAsset = CompanyAssetService.GetCompanyAssetByAssetId(Convert.ToInt64(ViewState["AssetId"]));

                    textBoxRemainingLifeTime.Text = (companyAsset.LifeTime - dateDifferent).ToString();

                }

                if (textBoxPurchasedDate.Text != string.Empty && textBoxPurchasedValue.Text != string.Empty)
                {
                    textBoxCumilativeDepValue.Text = (Convert.ToDecimal(textBoxDepreciationValue.Text) * Convert.ToInt64(ViewState["DateDifferent"])).ToString();
                    textBoxCurrentBookValue.Text = (Convert.ToDecimal(textBoxPurchasedValue.Text) - Convert.ToDecimal(textBoxCumilativeDepValue.Text)).ToString();
                }

            }


        }

        private void DataBindToMainCategory(List<Category> category, long mainCategoryId)
        {

            //category.Add((Category) categoryService.GetAssetCategoryById(Convert.ToInt64(arrayParameters[0])));

            dropDownListMainCategory.DataSource = category;
            dropDownListMainCategory.DataValueField = "CategoryId";
            dropDownListMainCategory.DataTextField = "Description";
            dropDownListMainCategory.DataBind();
            dropDownListMainCategory.SelectedValue = mainCategoryId.ToString();
        }

        private void DataBindToSubCategory(List<Category> category, long subCategoryId)
        {

            dropDownListSubCategory.DataSource = category;
            dropDownListSubCategory.DataValueField = "CategoryId";
            dropDownListSubCategory.DataTextField = "Description";
            dropDownListSubCategory.DataBind();
            dropDownListSubCategory.SelectedValue = subCategoryId.ToString();
        }

        private void GetDepriciationDetails(long assetId)
        {
            CompanyAssetService companyAssetService = new CompanyAssetService();
            CompanyAsset companyAsset = new CompanyAsset();
            companyAsset = companyAssetService.GetCompanyAssetByAssetId(assetId);
            textBoxDepreciationRate.Text = Convert.ToDecimal(companyAsset.DepreciationRate).ToString();
            textBoxDepreciationMethod.Text = Convert.ToInt64(companyAsset.DepreciationMethodId).ToString();

        }

        protected void buttonSave_OnClick(object sender, EventArgs e)
        {
            if (CheckMandatory())
            {
                AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
                AssetItemDetails assetItemDetails = new AssetItemDetails();
                OrganizationLevelListAssetService organizationLevelListAssetService = new OrganizationLevelListAssetService();
                OrganizationLevelListAsset organizationLevelListAsset = new OrganizationLevelListAsset();

                int count = Convert.ToInt16(ViewState["currentAssetDetails"]);



                //View State for checking whether values comes from OrganizationLevelListAssets or AssetsProcurement 
                if (ViewState["OLA"] == "OL")
                {
                    foreach (GridViewRow gridViewRow in gridViewItemDetails.Rows)
                    {
                        if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                        {

                            assetItemDetails.OrganizationLevelListId = Convert.ToInt64(ViewState["OrganizationLevelListId"]);
                            assetItemDetails.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                            string lastSerial = assetItemDetailsService.GetLastItemSerial(Convert.ToInt64(ViewState["AssetId"]));
                            int Serial = Convert.ToInt16(lastSerial) + 1;
                            //string AssetSerialCode=Serial
                            //This is for getting the last Serial from the DB
                            string s = string.Empty;
                            if (Serial < 10)
                            {
                                s = "0000";
                            }
                            else if (Serial < 100)
                            {
                                s = "000";
                            }
                            else if (Serial < 1000)
                            {
                                s = "00";
                            }
                            else if (Serial < 10000)
                            {
                                s = "0";
                            }

                            assetItemDetails.AssetSerialCode = s + Serial;

                            //assetItemDetails.AssetSerialCode = gridViewRow.Cells[1].Text;
                            assetItemDetails.AssetStatus = "AVB";
                            assetItemDetails.PurchasedDate = Convert.ToDateTime(textBoxPurchasedDate.Text);
                            assetItemDetails.PurchasedValue = Convert.ToDecimal(textBoxPurchasedValue.Text);
                            assetItemDetails.DepreciatedValue = Convert.ToDecimal(textBoxDepreciationValue.Text);
                            assetItemDetails.CumulativeDepValue = Convert.ToDecimal(textBoxCumilativeDepValue.Text);
                            assetItemDetails.CurrentBookValue = Convert.ToDecimal(textBoxCurrentBookValue.Text);
                            assetItemDetails.RemainingLifeTime = Convert.ToInt64(textBoxRemainingLifeTime.Text);
                            assetItemDetails.EmployeeID = Convert.ToInt64(Session["LoggedUserId"]);
                            assetItemDetails.Warranty = "";
                            assetItemDetails.Barcode = "";

                            assetItemDetailsService.AddAssetItemDetails(assetItemDetails);
                        }
                    }

                    organizationLevelListAsset.OrganizationLevelListId = Convert.ToInt64(ViewState["OrganizationLevelListId"]);
                    organizationLevelListAsset.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                    organizationLevelListAsset.NoOfAssets = Convert.ToInt16(ViewState["NumberOfAssets"]);

                    organizationLevelListAssetService.AddOrganizationLevelListAsset(organizationLevelListAsset);

                    DataBindToGridView();
                    // GetData(Convert.ToInt16(textBoxnoOfItems.Text), Convert.ToInt64(ViewState["AssetId"]), DataBindToGridView(), Convert.ToBoolean(ViewState["IsProcurement"]));
                    if (Convert.ToBoolean(ViewState["IsProcurement"]))
                    {
                        Response.Redirect("/OrganizationLevelListAssetForm.aspx");
                    }
                }
                else
                {
                    foreach (GridViewRow gridViewRow in gridViewItemDetails.Rows)
                    {
                        if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                        {
                            assetItemDetails.OrganizationLevelListId = Convert.ToInt64(ViewState["OrganizationLevelListId"]);
                            assetItemDetails.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                            //This is for getting Serial from the beggining 

                            assetItemDetails.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                            string lastSerial = assetItemDetailsService.GetLastItemSerial(Convert.ToInt64(ViewState["AssetId"]));

                            if (lastSerial != "")
                            {
                                int Serial = Convert.ToInt16(lastSerial) + 1;
                                //string AssetSerialCode=Serial
                                //This is for getting the last Serial from the DB
                                string s = string.Empty;
                                if (Serial < 10)
                                {
                                    s = "0000";
                                }
                                else if (Serial < 100)
                                {
                                    s = "000";
                                }
                                else if (Serial < 1000)
                                {
                                    s = "00";
                                }
                                else if (Serial < 10000)
                                {
                                    s = "0";
                                }

                                assetItemDetails.AssetSerialCode = s + Serial;

                            }
                            else
                            {
                                assetItemDetails.AssetSerialCode = gridViewRow.Cells[2].Text;
                            }



                            assetItemDetails.AssetStatus = "AVB";
                            assetItemDetails.PurchasedDate = Convert.ToDateTime(textBoxPurchasedDate.Text);
                            assetItemDetails.PurchasedValue = Convert.ToDecimal(textBoxPurchasedValue.Text);
                            assetItemDetails.DepreciatedValue = Convert.ToDecimal(textBoxDepreciationValue.Text);
                            assetItemDetails.CumulativeDepValue = Convert.ToDecimal(textBoxCumilativeDepValue.Text);
                            assetItemDetails.CurrentBookValue = Convert.ToDecimal(textBoxCurrentBookValue.Text);
                            assetItemDetails.RemainingLifeTime = Convert.ToInt64(textBoxRemainingLifeTime.Text);
                            assetItemDetails.EmployeeID = Convert.ToInt64(Session["LoggedUserId"]); ;
                            assetItemDetails.Warranty = "";
                            assetItemDetails.Barcode = "";

                            assetItemDetailsService.AddAssetItemDetails(assetItemDetails);
                        }
                    }



                    organizationLevelListAsset.OrganizationLevelListId = Convert.ToInt64(ViewState["OrganizationLevelListId"]);
                    organizationLevelListAsset.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                    organizationLevelListAsset.NoOfAssets = Convert.ToInt16(ViewState["NumberOfAssets"]);

                    organizationLevelListAssetService.ModifyOrganizationLevelListAssetByAssetId(organizationLevelListAsset);

                    DataBindToGridView();
                    // GetData(Convert.ToInt16(textBoxnoOfItems.Text), Convert.ToInt64(ViewState["AssetId"]), DataBindToGridView(), Convert.ToBoolean(ViewState["IsProcurement"]));
                    if (Convert.ToBoolean(ViewState["IsProcurement"]))
                    {
                        Response.Redirect("AssetProcurementForm.aspx?Parameter=" + Server.UrlEncode(textBoxAsset.Text) + ";" + Server.UrlEncode(dropDownListMainCategory.SelectedValue) + ";" + Server.UrlEncode(dropDownListSubCategory.SelectedValue) + ";" + Server.UrlEncode(textBoxnoOfItems.Text) + ";" + Server.UrlEncode(textBoxPurchasedValue.Text) + ";" + Convert.ToInt64(ViewState["AssetId"]));
                    }
                }
            }

        }


        private Boolean CheckMandatory()
        {

            DateTime today = Convert.ToDateTime(DateTime.Today);
            if (textBoxPurchasedDate.Text == string.Empty)
            {
                FlashMessage1.Message = "PurchaseDate Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (textBoxPurchasedValue.Text == string.Empty)
            {
                FlashMessage1.Message = "PurchaseValue Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (textBoxDepreciationValue.Text == string.Empty)
            {
                FlashMessage1.Message = "DepreciationValue Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (textBoxCumilativeDepValue.Text == string.Empty)
            {
                FlashMessage1.Message = "CumilativeDepValue Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (textBoxCurrentBookValue.Text == string.Empty)
            {
                FlashMessage1.Message = "CurrentBookValue Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (textBoxRemainingLifeTime.Text == string.Empty)
            {
                FlashMessage1.Message = "RemainingLifeTime Can't be Empty";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }
            if (Convert.ToDateTime(textBoxPurchasedDate.Text) > today)
            {
                FlashMessage1.Message = "PurchasedDate Can't be a Future Date";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
                return false;
            }

            return true;
        }


        protected void gridViewItemDetails_OnRowCreated(Object Sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {

                if (e.Row.Cells.Count > 1)
                {
                    e.Row.Cells[9].Visible = false;
                    e.Row.Cells[10].Visible = false;


                }

            }
        }
        //if (Convert.ToInt16(ViewState["currentAssetDetails"]) > 0)
        //{

        // }


        private void GetLastSerial()
        {
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            AssetItemDetails assetItemDetails = new AssetItemDetails();

        }

        private Collection<AssetItemDetails> DataBindToGridView()
        {
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            Collection<AssetItemDetails> assetItemDetails = new Collection<AssetItemDetails>();
            assetItemDetails = new Collection<AssetItemDetails>(assetItemDetailsService.GetAllAssetItemDetails().
                Where(x => x.AssetId == (Convert.ToInt64(ViewState["AssetId"]))).ToList());
            if (ViewState["OLA"] == "OL")
            {
            gridViewItemDetails.DataSource = assetItemDetails;
            gridViewItemDetails.DataBind();
            }
          

            return assetItemDetails;
        }

    }
}